package utils.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpRequest extends BaseRequest {
    /**
     * https的put的xml/json请求
     */
    public String httpsJsonPut(String url, Map<String,String> headMap, String val) throws Exception {
    	HttpPut httpPut = new HttpPut(url);
    	httpPut.setConfig(super.requestConfig);
		httpPut.addHeader("Content-type", "application/json; charset=utf-8");

		// 设置头属性
        setHeadMap(httpPut, headMap);

		httpPut.setEntity(new StringEntity(val, Charset.forName("UTF-8")));

		// 字符串的形式返回结果
    	return EntityUtils.toString(super.getHttpClient().execute(httpPut).getEntity());
    }

    /**
     *  https的post的xml/json请求
     */
    public String httpsJsonPost(String url, Map<String,String> headMap, String val) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	httpPost.setConfig(requestConfig);
    	httpPost.addHeader("Content-type", "application/json; charset=utf-8");

        // 设置头属性
        setHeadMap(httpPost, headMap);
		
		httpPost.setEntity(new StringEntity(val,Charset.forName("UTF-8")));

        // 字符串的形式返回结果
    	return EntityUtils.toString(super.getHttpClient().execute(httpPost).getEntity());
    }
    /*public String httpsJsonPost(String url, Map<String,String> headMap, String val, String time) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	if(time!=null){
    		httpPost.setConfig(requestConfig);
    	}
    	httpPost.addHeader("Content-type","application/json; charset=utf-8");
		HttpEntity entity = new StringEntity(val,Charset.forName("UTF-8"));

        setHeadMap(httpPost, headMap);
		
		httpPost.setEntity(entity);
        HttpResponse httpResponse = super.getHttpClient().execute(httpPost);
    	return EntityUtils.toString(httpResponse.getEntity());
    }*/

    /**
     *  https的get的请求
     */
    // 返回字符串结果
    public String httpsGet(String url) throws Exception {
    	HttpGet httpGet = new HttpGet(url);
    	httpGet.setConfig(requestConfig);
    	httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 字符串的形式返回结果
    	return EntityUtils.toString(super.getHttpClient().execute(httpGet).getEntity());
    }
    // 返回字符串结果
    public String httpsGet(String url, Map<String,String> headMap) throws Exception {
    	HttpGet httpGet = new HttpGet(url);
    	httpGet.setConfig(requestConfig);
    	httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 设置头属性
        setHeadMap(httpGet, headMap);

        // 字符串的形式返回结果
    	return EntityUtils.toString(super.getHttpClient().execute(httpGet).getEntity());
    }
    // 返回输入流
    public InputStream httpsGetStream(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 返回输入流
        return super.getHttpClient().execute(httpGet).getEntity().getContent();
    }
    // 返回输入流
    public InputStream httpsGetStream(String url, Map<String,String> headMap) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 设置头属性
        setHeadMap(httpGet, headMap);

        // 返回输入流
        return super.getHttpClient().execute(httpGet).getEntity().getContent();
    }

    /**
     *  http的get的请求
     */
    // 返回字符串结果
    public String httpGet(String url) throws Exception {
    	HttpGet httpGet = new HttpGet(url);
    	httpGet.setConfig(requestConfig);
    	httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 字符串的形式返回结果
    	return EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());
    }
    // 返回输入流
    public InputStream httpGetStream(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        httpGet.addHeader("Content-type", "application/json; charset=utf-8");

        // 返回输入流
        return new DefaultHttpClient().execute(httpGet).getEntity().getContent();
    }
    /**
     *  http的post的xml/json请求
     */
    public String httpPost(String url, Map<String,String> headMap, String val) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	httpPost.setConfig(requestConfig);
    	httpPost.addHeader("Content-type","application/json; charset=utf-8");

        // 设置头属性
        setHeadMap(httpPost, headMap);
		
		httpPost.setEntity(new StringEntity(val,Charset.forName("UTF-8")));

        // 字符串的形式返回结果
    	return EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity());
    }

    // 传入完整的header
    public String httpPostHeader(String url, Map<String,String> headMap, String val) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        HttpEntity entity=new StringEntity(val,Charset.forName("UTF-8"));

        // 设置头属性
        setHeadMap(httpPost, headMap);

        httpPost.setEntity(entity);

        // 字符串的形式返回结果
        return EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity());
    }

    // 传入完整的header，返回 Stream 流
    public InputStream httpPostHeaderStream(String url, Map<String,String> headMap, String val) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        HttpEntity entity=new StringEntity(val,Charset.forName("UTF-8"));

        // 设置头属性
        setHeadMap(httpPost, headMap);

        httpPost.setEntity(entity);

        // 返回输入流
        return new DefaultHttpClient().execute(httpPost).getEntity().getContent();
    }

    public String httpPost(String url, Map<String,String> headMap, String val, String time) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	if(time!=null){
    		httpPost.setConfig(requestConfig);
    	}
    	httpPost.addHeader("Content-type","application/json; charset=utf-8");
		HttpEntity entity=new StringEntity(val,Charset.forName("UTF-8"));

        // 设置头属性
        setHeadMap(httpPost, headMap);
		
		httpPost.setEntity(entity);

        // 字符串的形式返回结果
    	return EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity(), "utf-8");
    }
    public String httpPost(String url, String val) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	httpPost.setConfig(requestConfig);
    	httpPost.addHeader("Content-type","application/json");//application/x-www-form-urlencoded; charset=UTF-8
    	HttpEntity entity=new StringEntity(val,Charset.forName("UTF-8"));
		
		httpPost.setEntity(entity);
    	return EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity(), "utf-8");
    }
    /**
     * http/https 的 From表单/post请求
     */
    public String httpFromPost(String url, List<BasicNameValuePair> qparams, String httpType) throws Exception {
    	HttpPost httpPost = new HttpPost(url);
    	httpPost.setConfig(requestConfig);
    	httpPost.setEntity(new UrlEncodedFormEntity(qparams, "utf-8"));
    	HttpClient httpClient =null;
    	if(httpType.equals("http")){
    		httpClient = new DefaultHttpClient();
    	}else if(httpType.equals("https")){
    		httpClient = getHttpClient();
    	}else{
    		throw new Exception("httpType 的值为http或https");
    	}
    	return EntityUtils.toString(httpClient.execute(httpPost).getEntity(), "utf-8");
    }
    
    /*public static void main(String[] args) throws Exception {
    	String url="http://sys.etg56.com/client/quote/inquiryQuote.do";
    	List<BasicNameValuePair> qparams=new ArrayList<BasicNameValuePair>();
    	qparams.add(new BasicNameValuePair("clientId", "8a2af70150d0e3320150f9ab73b80121"));
    	qparams.add(new BasicNameValuePair("des", "US"));
    	qparams.add(new BasicNameValuePair("actWeight", "1"));
    	qparams.add(new BasicNameValuePair("itemType", "SPX"));
    	qparams.add(new BasicNameValuePair("batteryFlag", ""));
    	qparams.add(new BasicNameValuePair("start", "0"));
    	qparams.add(new BasicNameValuePair("limit", "50"));
    	String httpType="http";
    	
    	HttpRequest httpRequest=new HttpRequest();
    	String str=httpRequest.httpFromPost(url,qparams,httpType);
    	JSONObject jo = JSONObject.fromObject(str);
    	System.out.println(jo.toString());
	}*/



    /**
     * 设置头部信息
     */
    private void setHeadMap(Object objPost, Map<String,String> headMap) {
        if(headMap==null || headMap.size()==0) {
            return;
        }
        Iterator<Map.Entry<String,String>> iterator = headMap.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String,String> entry = iterator.next();
            if(objPost instanceof HttpPut) {

                ((HttpPut) objPost).addHeader(entry.getKey(), entry.getValue());
            } else if(objPost instanceof HttpPost) {

                ((HttpPost) objPost).addHeader(entry.getKey(), entry.getValue());
            }  else if(objPost instanceof HttpGet) {

                ((HttpGet) objPost).addHeader(entry.getKey(), entry.getValue());
            } else {
                return;
            }
        }
    }
}
